package org.qf.dao.impl;

import org.qf.dao.SmbmsOrderDao;
import org.qf.pojo.SmbmsBill;
import org.qf.pojo.SmbmsBillItem;
import org.qf.utils.BaseDao;

import java.sql.Connection;
import java.util.List;

/**
 * @author zzzyyystart
 * @create 2022-05-11-13:25
 */
public class SmbmsOrderImpl extends BaseDao implements SmbmsOrderDao {
    @Override
    public List<SmbmsBill> showBillList() {
        Connection conn = null;
        try {
            conn = getConnection1();
            String sql = "select `order_code` orderCode,`create_time` createTime," +
                    "`price` totalPrice,`status`,`user_id` userId from t_order";
            return queryForList(conn, sql, SmbmsBill.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return null;
    }

    @Override
    public List<SmbmsBillItem> showBillDetail(String orderCode) {
        Connection conn = null;
        try {
            conn = getConnection1();
            String sql = "select `item_id` id,`order_code` orderCode,`name`,`count`," +
                    "`unit_price` unitPrice,`total_price` totalPrice from t_order_item where" +
                    "`order_code`=?";
            return queryForList(conn, sql, SmbmsBillItem.class, orderCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return null;
    }

    @Override
    public int updateBillStatus(String orderId) {
        Connection conn = null;
        try {
            conn = getConnection1();
            String sql = "update `t_order` set status=? where `order_code`=?";
            return update(conn, sql, "SHIPPED", orderId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return -1;
    }

    @Override
    public List<SmbmsBill> queryForListByStatus(String billStatus) {
        Connection conn = null;
        try {
            conn = getConnection1();
            String sql = "select `order_code` orderCode,`create_time` createTime,`price` totalPrice,`status` status," +
                    "`user_id` userId from t_order where `status`=?";
            return queryForList(conn, sql, SmbmsBill.class, billStatus);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return null;
    }
}